home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / 1.098 / rpc / rpcByteSwap.c < prev    next >
C/C++ Source or Header  |  1991-02-12  |  5KB  |  197 lines

  1. /*
  2.  * rpcByteSwap.c --
  3.  *
  4.  *    The code to byte swap incoming rcp headers and parameter blocks.
  5.  *
  6.  * Copyright 1988 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /sprite/src/kernel/rpc/RCS/rpcByteSwap.c,v 9.3 91/02/12 14:15:48 jhh Exp $ SPRITE (Berkeley)";
  18. #endif /* not lint */
  19.  
  20.  
  21. #include <sprite.h>
  22. #include <stdio.h>
  23. #include <net.h>
  24. #include <rpcInt.h>
  25. #include <rpcPacket.h>
  26.  
  27. /*
  28.  * Global var for testing byte-swapping.  Causes out-going messages to
  29.  * be byte-swapped, so that the receiving machine must byte-swap them if
  30.  * it is of the same sort.
  31.  */
  32. Boolean    rpcTestByteSwap = FALSE;
  33.  
  34.  
  35. /*
  36.  *----------------------------------------------------------------------
  37.  *
  38.  * RpcByteSwapBuffer --
  39.  *
  40.  *    Byte swap the given buffer integer-by-integer.
  41.  *    Byte swapping means taking an integer of the form 0x61626364 and
  42.  *    turning it into 0x64636261.
  43.  *
  44.  * Results:
  45.  *    None.
  46.  *
  47.  * Side effects:
  48.  *    The buffer is byte-swapped.
  49.  *
  50.  *----------------------------------------------------------------------
  51.  */
  52. void
  53. RpcByteSwapBuffer(bufferPtr, numInts)
  54.     register int *bufferPtr;
  55.     register int numInts;
  56. {
  57.     register unsigned int in;
  58.  
  59. #define    LOW_BYTE_MASK        0x000000ff
  60. #define    MIDDLE_LOW_BYTE_MASK    0x0000ff00
  61. #define    MIDDLE_HIGH_BYTE_MASK    0x00ff0000
  62. #define    HIGH_BYTE_MASK        0xff000000
  63.  
  64.     while (--numInts >= 0) {
  65.     in = *bufferPtr;
  66.     *bufferPtr++ =   ((in << 8)  & MIDDLE_HIGH_BYTE_MASK)
  67.                    | ((in << 24) & HIGH_BYTE_MASK)
  68.                | ((in >> 8)  & MIDDLE_LOW_BYTE_MASK)
  69.                | ((in >> 24) & LOW_BYTE_MASK);
  70.     }
  71.     return;
  72. }
  73.  
  74.  
  75.  
  76. /*
  77.  *----------------------------------------------------------------------
  78.  *
  79.  * RpcByteSwapInComing --
  80.  *
  81.  *    Byte swap the in-coming rpc packet header and the following parameter
  82.  *    block, integer-by-integer.  The data block is not byte swapped.
  83.  *    It knows the RPC packet format:
  84.  *        The Rpc header, which includes the sizes of the next two parts.
  85.  *        The parameter area.
  86.  *        The data area.
  87.  *    Byte swapping means taking an integer of the form 0x61626364 and turning
  88.  *    it into 0x64636261.
  89.  *
  90.  * Results:
  91.  *    TRUE if successful, FALSE if not.  Maybe this should require a more
  92.  *    interesting set of return values, but Rpc_Dispatch just drops the
  93.  *    packet if this routine returns FALSE.
  94.  *
  95.  * Side effects:
  96.  *    The header packet structure and the parameter block are byte-swapped.
  97.  *
  98.  *----------------------------------------------------------------------
  99.  */
  100. Boolean
  101. RpcByteSwapInComing(rpcHdrPtr, packetLength)
  102.     RpcHdr    *rpcHdrPtr;        /* The Rpc Header as it sits in the
  103.                      * network's buffer.  The data follows
  104.                      * the header directly. */
  105.     int        packetLength;        /* Length of the network packet. */
  106. {
  107.     int    numInts;
  108.     int    paramSize;
  109.  
  110.     /*
  111.      * First byte-swap the rpc header itself.  The header had better be an
  112.      * integral number of integers long!
  113.      */
  114.     numInts = sizeof (RpcHdr) / sizeof (int);
  115.     if ((sizeof (RpcHdr) % sizeof (int)) != 0) {
  116.     return FALSE;
  117.     }
  118.     RpcByteSwapBuffer((int *) rpcHdrPtr, numInts);
  119.  
  120.     /* now that we've got the right values */
  121.     paramSize = rpcHdrPtr->paramSize;
  122.  
  123.     /*
  124.      * Make sure the parameter block is all there.
  125.      */
  126.     if (packetLength < sizeof(RpcHdr) + rpcHdrPtr->paramSize) {
  127.     printf("RpcByteSwapInComing: SHORT packet %d < %d, ", 
  128.         packetLength, sizeof(RpcHdr) + rpcHdrPtr->paramSize);
  129.     printf("srv %d clt %d rpc %d\n", rpcHdrPtr->serverID,
  130.             rpcHdrPtr->clientID, rpcHdrPtr->command);
  131.     return FALSE;
  132.     }
  133.     /*
  134.      * Now byte-swap the parameter block.  If it isn't an integral number of
  135.      * integers long, something is wrong.
  136.      */
  137.     numInts = paramSize / sizeof (int);
  138.     if ((paramSize % sizeof (int)) != 0) {
  139.     printf("RpcByteSwapInComing: bad paramSize %d, ", paramSize);
  140.     printf("srv %d clt %d rpc %d\n", rpcHdrPtr->serverID,
  141.             rpcHdrPtr->clientID, rpcHdrPtr->command);
  142.     return FALSE;
  143.     }
  144.     /*
  145.      * We don't add on paramOffset here, since the parameter area, even if
  146.      * it's a fragment, immediately follows the rpcHdr for the fragment.
  147.      */
  148.     RpcByteSwapBuffer((int *) (((char *) rpcHdrPtr) + sizeof (RpcHdr)),
  149.         numInts);
  150.  
  151.     return TRUE;
  152. }
  153.  
  154. /*
  155.  *----------------------------------------------------------------------
  156.  *
  157.  * RpcPrintHdr --
  158.  *
  159.  *    Print out fields from the given header.
  160.  *
  161.  * Results:
  162.  *    None.
  163.  *
  164.  * Side effects:
  165.  *    printf is invoked.
  166.  *
  167.  *----------------------------------------------------------------------
  168.  */
  169. void
  170. RpcPrintHdr(rpcHdrPtr)
  171.     RpcHdr    *rpcHdrPtr;        /* The Rpc Header as it sits in the
  172.                      * network's buffer.  The data follows
  173.                      * the header directly. */
  174. {
  175.  
  176.     printf("\tversion(X): %x\n\tflags(X): %x\n\tclientID(D): %d\n",
  177.         rpcHdrPtr->version, rpcHdrPtr->flags, rpcHdrPtr->clientID);
  178.         printf("\tcommand(D): %d\n\tparamSize(X): %x\n", rpcHdrPtr->command,
  179.         rpcHdrPtr->paramSize);
  180.         printf("\tparamOffset(X): %x\n", rpcHdrPtr->paramOffset);
  181.     return;
  182. }
  183.  
  184. int
  185. RpcSetTestByteSwap()
  186. {
  187.     rpcTestByteSwap = 1;
  188.     return 0;
  189. }
  190.  
  191. int
  192. RpcUnsetTestByteSwap()
  193. {
  194.     rpcTestByteSwap = 0;
  195.     return 0;
  196. }
  197.